package com.dearbinge.admin.client.openapi.controller.securityauthority;

import com.alibaba.fastjson.JSONArray;
import com.dearbinge.data.api.admin.AdminSecurityAuthorityService;
import com.dearbinge.data.api.admin.AdminSecurityRoleAuthorityService;
import com.dearbinge.data.pojo.admin.vo.AdminSecurityAuthorityRequestVO;
import com.dearbinge.data.pojo.admin.vo.AdminSecurityAuthorityResponseVO;
import com.dearbinge.data.pojo.admin.vo.AdminSecurityRoleAuthorityRequestVO;
import com.dearbinge.utils.BeanValidator;
import com.dearbinge.utils.ResultMsg;
import com.dearbinge.utils.enums.ResultStatusCodeEnum;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台权限表控制层
 *
 * @author wes
 * @date 2019/1/10
 */
@Api(tags = "1.1、权限-后台权限")
@RestController
@RequestMapping("/securityAuthority")
@CrossOrigin
@EnableAutoConfiguration
public class SecurityAuthorityController extends WebMvcConfigurerAdapter {

    @Autowired
    private AdminSecurityAuthorityService adminSecurityAuthorityService;
    @Autowired
    private AdminSecurityRoleAuthorityService adminSecurityRoleAuthorityService;

    @ApiOperation(value = "根据ID删除", notes = "根据ID删除")
    @ApiImplicitParam(name = "authorityId", value = "权限表id", paramType = "query", required = true)
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public ResultMsg<String> delete(Integer authorityId) {
        try {
            Integer dataCode = adminSecurityAuthorityService.delete(authorityId);
            if (dataCode > 0) {
                return ResultMsg.ok("删除成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(500, "系统异常");
    }


    @ApiOperation(value = "根据id查询", notes = "根据id查询")
    @ApiImplicitParam(name = "authorityId", value = "表id", paramType = "query", required = true)
    @ApiResponses({
            @ApiResponse(response = AdminSecurityAuthorityResponseVO.class, code = 200, message = "返回结果参数")
    })
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    public ResultMsg<AdminSecurityAuthorityResponseVO> getAddressById(Integer authorityId) {
        try {
            AdminSecurityAuthorityResponseVO result = adminSecurityAuthorityService.getById(authorityId);
            return ResultMsg.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(500, "系统异常");
    }


    @ApiOperation(value = "增加修改", notes = "增加修改")
    @RequestMapping(value = "/addOrUpdate", method = RequestMethod.POST)
    public ResultMsg<String> addOrUpdate(@RequestBody AdminSecurityAuthorityRequestVO adminSecurityAuthorityRequestVO) {
        try {
            Integer dataCode;
            if (adminSecurityAuthorityRequestVO.getAuthorityId() == null || adminSecurityAuthorityRequestVO.getAuthorityId() == 0) {

                dataCode = adminSecurityAuthorityService.insert(adminSecurityAuthorityRequestVO);
                if (dataCode > 0) {
                    return ResultMsg.ok("增加成功");
                }
            } else {
                dataCode = adminSecurityAuthorityService.update(adminSecurityAuthorityRequestVO);
                if (dataCode > 0) {
                    return ResultMsg.ok("修改成功");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), ResultStatusCodeEnum.CODE500.getDesc());
    }


    @ApiOperation(value = "查询全部权限", notes = "查询全部权限")
    @ApiImplicitParam(name = "roleId", value = "角色id", paramType = "query", required = true)
    @RequestMapping(value = "/listAddressByUserId", method = RequestMethod.GET)
    public ResultMsg<JSONArray> listAddressByUserId(Integer roleId) {
        try {
            JSONArray list = adminSecurityAuthorityService.listAll(roleId);
            return ResultMsg.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(500, "系统异常");
    }


    @ApiOperation(value = "保存角色权限", notes = "保存角色权限")
    @RequestMapping(value = "/addRoleAuthority", method = RequestMethod.POST)
    public ResultMsg<String> addRoleAuthority(@RequestBody List<AdminSecurityRoleAuthorityRequestVO> list) {
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>(16);
            for (AdminSecurityRoleAuthorityRequestVO adminSecurityRoleAuthorityRequestVO : list) {
                paramMap.put("roleId", adminSecurityRoleAuthorityRequestVO.getRoleId());
                paramMap.put("authorityId", adminSecurityRoleAuthorityRequestVO.getAuthorityId());
                Map<String, Object> checkParam = BeanValidator.checkParam(paramMap);
                if (!(boolean) checkParam.get(BeanValidator.FLAG)) {
                    return ResultMsg.error(ResultStatusCodeEnum.CODE5001.getCode(), checkParam.get("name") + "参数为空");
                }
            }
            Integer  dataCode = adminSecurityRoleAuthorityService.insert(list);
            if (dataCode > 0) {
                return ResultMsg.ok("保存成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), ResultStatusCodeEnum.CODE500.getDesc());
    }


}

